import { message, notification, Tag } from 'ant-design-vue'
import { h } from 'vue'
import EventBus from './EventBus'

EventBus.on('CodeError', (code: number, msg?: any) => {
    switch (code) {
        case 400:
            message.error(msg)
            break;
        case 401:
            message.error('token验证已过期，请重新登录')
            break;
        case 403:
            message.error('token不存在，请重新登录')
            break;
        case -1:
            message.error('服务异常，请稍后重试！')
            break;
        default:
            message.error('服务异常，请稍后重试！')
    }
    return 123
})

EventBus.on('ServerError', () => message.error('服务异常，请稍后重试！'))
EventBus.on('LoginSuccess', (name: string) => message.success('登录成功！'))

EventBus.on('CancleInfo', () => message.info('已取消'))
EventBus.on('AddSuccess', () => message.success('添加成功！'))
EventBus.on('UploadSuccess', () => message.success('上传成功！'))
EventBus.on('UpdateSuccess', () => message.success('修改成功！'))
EventBus.on('DeleteSuccess', () => message.success('已删除！'))

EventBus.on("ShowNotification", (data: any) =>
    notification.warning({
        duration: null,
        message: `设备报警`,
        description: h('div', null, [
            h('p', null, [`报警设备：`, h(Tag, { color: '#108ee9' }, { default: () => data.deviceName })]),
            h('p', null, [`序列号：`, h('span', null, [data.serial])]),
            h('p', null, [`报警类型：`, h(Tag, { color: '#f50' }, { default: () => data.alarmType })]),
        ])
    })
)